using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MindFactorial.LaunchPoint.Common;

namespace MindFactorial.LaunchPoint.Console
{
    /// <summary>
    /// Interaction logic for LoginPage.xaml
    /// </summary>

    public partial class LoginPage : System.Windows.Controls.Page
    {
        ConsoleWindow _window;
        System.Collections.Hashtable languages;
        System.Collections.Hashtable cultureInfo;
        public LoginPage(ConsoleWindow window)
        {
            //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ja-JP");
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(LoginPage_Loaded);
            _window = window;

            /*
            languages = new System.Collections.Hashtable();
            languages.Add("English", "en-US");
            languages.Add("Japanese", "ja-JP");
            languages.Add("Chinese", "zh-Hans");
            languages.Add("Spanish", "es-ES");
            languages.Add("German", "de-DE");
            languages.Add("French", "fr-FR");

            cultureInfo = new System.Collections.Hashtable();
            cultureInfo.Add("en-US","1");
            cultureInfo.Add("ja-JP", "2");
            cultureInfo.Add("zh-Hans", "3");
            cultureInfo.Add("es-ES", "4");
            cultureInfo.Add("de-DE", "5");
            cultureInfo.Add("fr-FR", "6");
            */ 
        }

        void LoginPage_Loaded(object sender, RoutedEventArgs e)
        {
            _window.WindowTitle = Properties.Resources.lblLogin;
            this.btnLogin.Click += new RoutedEventHandler(btnLogin_Click);
        }

        void btnLogin_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                /*
                //************************Set Database Locale**************************
                string current_culture = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
                if (cultureInfo.ContainsKey(current_culture))
                {
                    string parameter = string.Format("<Language>{0}</Language>", cultureInfo[current_culture].ToString());
                    string response = _window.RunnableContext.Context.RulesEngine.Rule["FactoryExplorerAdapter", "SetLanguage"].Execute(parameter);
                }
                //************************Set Database Locale**************************
                */

                //TODO: call BRE SECURITY SERVICE
                if (txtUsername.Text.Length > 0)
                {

                    /*
                    //****************Switch Cultute with dropdown***********************
                    if (languages.ContainsKey(cboLanguage.Text))
                    {
                        if (cultureInfo.ContainsKey(languages[cboLanguage.Text].ToString()))
                        {
                            string cultureID = languages[cboLanguage.Text].ToString();
                            string languageID = cultureInfo[cultureID].ToString();
                            string argument = string.Format("<Language>{0}</Language>", languageID);
                            string result = _window.RunnableContext.Context.RulesEngine.Rule["FactoryExplorerAdapter", "SetLanguage"].Execute(argument);
                            if (!result.Contains("Success"))
                            {
                                MessageBox.Show("Language Could not be set.");
                            }
                            else
                            {
                                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cultureID);
                                _window.SetLanguage(cultureID);
                            }
                        }
                    }
                    //****************Switch Cultute with dropdown***********************
                     */
                    _window.SetCurrentUser(this.txtUsername.Text);
                    _window.NavigateToLaunchpad();
                }
                else
                {
                    MessageBox.Show(MindFactorial.LaunchPoint.Console.Properties.Resources.msgUserNameRequired);
                }
            }
            catch(Exception ex)
            {
                Logger.Log("AF6C19A3-654A-47ce-AA90-4D54FA03722K: Error description : " + ex.Message);
            }
        }

       

    }
}